<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Game\Controller;

use Think\Controller;

class ApplyController extends CommonController {

    function index() {
//        header("Content-type: text/html; charset=utf-8");       
//        $openid = 'o3Iz7jmt0LH1OCKTjBibEi9CHT7o';
//        $rs = check_vip($openid);
//        echo getCodeNameByHouse($rs['house']);
        //echo wx_openid();exit;
        $mod = M('Join');
        $openid = wx_openid();
        $rs = check_vip($openid); //是否是会员
        $where['tel'] = $rs['huiyuan_tel'];
        $where['openid'] = $openid; //$rs['huiyuan_tel'];
        $isjoin = $mod->where($where)->find(); //是否参加
        //会员
        if ($rs) {
            if ($isjoin) {
                //已参加报名
                $this->assign('data', $isjoin);
                $this->display('success');
            } else {
                //未参加报名
                $this->display('index');
            }
        } else {
            $bool = R('Api/Fans/updateFansByOpenid', array(wx_openid()));
            if ($bool === true) {
                redirect('http://a.forcent.cn/iHuiyuan/user/Integral?url=' . urlencode('http://a.forcent.cn/ldh/vote.php/Apply?key=ALDJSkjhasd'));
            } else {
                redirect('http://mp.weixin.qq.com/s?biz=MjM5ODg1MDI2OA==&mid=209800739&idx=1&sn=54b350a36b6655303eadca02c425ddb9#rd');
            }
        }
    }

    //异步添加数据
    function doAdd() {
        // echo $_REQUEST['hobby'];exit;
        $mod = M('Join');
        $openid = wx_openid();
        $rs = check_vip($openid); //是否是会员
        $dataArr['tel'] = $rs['huiyuan_tel'];
        $dataArr['name'] = $rs['huiyuan_name'];
        $dataArr['code_name'] = getCodeNameByHouse($rs['house']);
        $dataArr['jointime'] = rtrim($_REQUEST['jointime'], ' / ');
        $dataArr['joinhobby'] = rtrim($_REQUEST['hobby'], ' / ');
        $dataArr['openid'] = $rs['open_id'];
        $dataArr['card_no'] = $rs['card_no'];
        $dataArr['ctime'] = date('Y-m-d H:i:s');
        $state = $mod->add($dataArr);
        if ($state) {
            $data['status'] = 1;
            $data['url'] = 'http://a.forcent.cn/ldh/vote.php/Apply';
            $this->ajaxReturn($data);
        } else {
            $data['status'] = 0;
            $data['info'] = "网络异常,请重新填写!";
            $this->ajaxReturn($data);
        }
    }

}
